package com.ugoodtech.mdcc.client.dto;

import org.slf4j.MDC;

import java.util.HashMap;
import java.util.Map;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * Created by Lance on 2019-07-23.
 */

@Slf4j
@Data
public class ServerResponse {

  public static final String CODE_SUCCESS = "1000";
  public static final String CODE_ERROR = "1001";
  public static final String CODE_AUTH_ERROR = "1002";
  public static final String CODE_ACCESS_ERROR = "1003";
  public static final String CODE_REMOTE_ERROR = "1004";
  public static final String CODE_REPEAT_ERROR = "1005";
  public static final String CODE_FORM_ERROR = "1006";
  public static final String CODE_STORAGE_ERROR = "1007";
  public static final String CODE_TOO_MANY_ERROR = "1008";
  public static final String CODE_NOT_FOUND = "1009";

  public static final String MSG_SUCCESS = "操作成功";
  public static final String MSG_FAILED = "操作失败";
  public static final String MSG_FORM_ERROR = "表单验证失败";

  private String code;
  private String msg;
  private Object data;
  private String sid;         // 防止表单重复提交
  private long timestamp;     // 提交时间戳

  public static ServerResponse errorStatic(String msg, Object data) {
    Map resultData = new HashMap<String, Object>();
    resultData.put("errorInfo", data);
    return error(CODE_ERROR, msg, resultData);
  }

  /**
   * 操作成功(1001)
   */
  public static ServerResponse success(String msg, Object data) {
    Map resultData = new HashMap<String, Object>();
    // Wrap data with 'dataInfo'.
    resultData.put("dataInfo", data);
    return defaultServerResponse(CODE_SUCCESS, msg, resultData, System.currentTimeMillis());
  }

  /**
   * 操作失败(1001)
   *
   * msg 简单提示 系统提示
   */
  public static ServerResponse error(String msg, Object data) {
    Map resultData = new HashMap<String, Object>();
    resultData.put("errorInfo", data);
    return error(CODE_ERROR, msg, resultData);
  }

  /**
   * 操作失败(1002 - 10xx)
   *
   * 当前配置文件是dev环境允许返回错误内容 当前配置文件是pro环境则屏蔽错误内容 所有错误请求存入日志
   */
  public static ServerResponse error(String code, String msg, Object data) {
    if (log.isDebugEnabled()) {
      log.debug("code: {},desc: {}, data: {}", code, msg, data);
    }
//        var profile = Arrays.stream(SpringContext.getApplicationContext().getEnvironment().getActiveProfiles()).findFirst();
//
//        if (profile.isPresent()) {
//            if (!"dev".equals(profile.get()) && !"local".equals(profile.get())&& !"blank".equals(profile.get())) {
//                data = new HashMap<String, String>();
//            }
//        }
    Map resultData = new HashMap<String, Object>();
    resultData.put("errorInfo", data);
    return defaultServerResponse(code, msg, resultData, System.currentTimeMillis());
  }


  /**
   * 默认封装
   */
  private static ServerResponse defaultServerResponse(
      String code, String msg, Object resultData, Long timestamp) {
    ServerResponse serverResponse = new ServerResponse();
    serverResponse.setCode(code);
    serverResponse.setMsg(msg);
    serverResponse.setData(resultData);
    serverResponse.setSid(MDC.get("requestId"));
    serverResponse.setTimestamp(timestamp);
    return serverResponse;
  }


}
